NeovimでScalaの編集環境を整えてみた

NeovimでScalaの編集環境を整えてみた

Scalaの編集環境を整える必要があり、シンタックスハイライトと補完を行うべく設定を追加してみました。時間がそれなりに掛かります。
Clock Icon2020.06.01

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

はじめに

Scalaを使う場合に基本はIDEを使うと思われますが、色々最適化したvimで済ませたくなり、導入手順を調べつつ色々と入れてみました。環境はNeovim 0.4.3になります。

入れたもの

  • sbt(homebrew)
  • ensime-vim(neovim)

既に入っていたもの

  • ale(neovim)
  • deoplete(neovim)
  • pynvim(pipy)
  • websocket-client(pipy)
  • sexpdata(pipy)

導入

結構時間がかかります。

sbt

% brew install sbt
==> Downloading https://homebrew.bintray.com/bottles/openjdk-13.0.2%2B8_2.catalina.bottle.tar.gz
==> Downloading from https://akamai.bintray.com/65/65adca036393f528e3830cab8b0aafec94be870de087d94cfe098fd593517307?__gda__=exp=1591004933~hmac=3c708fb3cb7150f50d234c27bac3f
################################################################## 100.0%
==> Downloading https://github.com/sbt/sbt/releases/download/v1.3.12/sbt-1.3.12.tgz
==> Downloading from https://github-production-release-asset-2e65be.s3.amazonaws.com/279553/f0d03000-a2d9-11ea-8b2c-fb650ce48d8d
################################################################## 100.0%
==> Installing dependencies for sbt: openjdk
==> Installing sbt dependency: openjdk
==> Pouring openjdk-13.0.2+8_2.catalina.bottle.tar.gz
==> Caveats
For the system Java wrappers to find this JDK, symlink it with
  sudo ln -sfn /usr/local/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk
openjdk is keg-only, which means it was not symlinked into /usr/local,
because it shadows the macOS <code>java</code> wrapper.
If you need to have openjdk first in your PATH run:
  echo 'export PATH="/usr/local/opt/openjdk/bin:$PATH"' >> ~/.zshrc
For compilers to find openjdk you may need to set:
  export CPPFLAGS="-I/usr/local/opt/openjdk/include"
==> Summary
?  /usr/local/Cellar/openjdk/13.0.2+8_2: 631 files, 314.6MB
==> Installing sbt
==> Caveats
You can use $SBT_OPTS to pass additional JVM options to sbt.
Project specific options should be placed in .sbtopts in the root of your project.
Global settings should be placed in /usr/local/etc/sbtopts
==> Summary
?  /usr/local/Cellar/sbt/1.3.12: 805 files, 61.4MB, built in 5 seconds
==> Caveats
==> openjdk
For the system Java wrappers to find this JDK, symlink it with
  sudo ln -sfn /usr/local/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk
openjdk is keg-only, which means it was not symlinked into /usr/local,
because it shadows the macOS <code>java</code> wrapper.
If you need to have openjdk first in your PATH run:
  echo 'export PATH="/usr/local/opt/openjdk/bin:$PATH"' >> ~/.zshrc
For compilers to find openjdk you may need to set:
  export CPPFLAGS="-I/usr/local/opt/openjdk/include"
==> sbt
You can use $SBT_OPTS to pass additional JVM options to sbt.
Project specific options should be placed in .sbtopts in the root of your project.
Global settings should be placed in /usr/local/etc/sbtopts

ensime-vim

今回はdein.tomlに追記する形です。

[[plugins]]
repo = 'ensime/ensime-vim'

ale

scala用の指定を追加します。また、dependsにてensime-vimを指定します。

hook_add = '''
    let b:ale_linters = {
    \   'scala': ['fsc', 'sbtserver', 'scalac', 'scalastyle']
    }
'''
depends = ['ensime-vim']

deoplete

既にdeopleteでhook_sourceの設定が存在する場合は追記していきます。

hook_source = '''
  call deoplete#custom#option('sources', {
       \ 'scala': ['buffer', 'dictionary', 'file', 'member', 'omni', 'tag'],
       })
  call deoplete#custom#var('omni', 'input_patterns', {
       \ 'scala': [
       \     '[^. *\t].\w*',
       \     '[:[,] ?\w*',
       \     '^import .*'
       \   ]
       })
'''

以下のコマンドを実行して完了です。

:UpdateRemotePlugins

sbtの設定

まずは設定ファイルを作成します。

% mkdir -p ~/.sbt/1.0/plugins/
% echo 'addSbtPlugin("org.ensime" % "sbt-ensime" % "2.5.1")' >  ~/.sbt/1.0/plugins/plugins.sbt
% echo 'serverConnectionType := ConnectionType.Tcp' > ~/.sbt/1.0/global.sbt
% echo 'serverPort := 4273' >> ~/.sbt/1.0/global.sbt

その後、リポジトリ内でconfig設定用のコマンドを実行します。途中で通信失敗するケースもあるため、成功まで何度か繰り返しましょう。

% sbt ensimeConfig
[info] [launcher] getting org.scala-sbt sbt 1.3.12  (this may take some time)...
:: loading settings :: url = jar:file:/usr/local/Cellar/sbt/1.3.12/libexec/bin/sbt-launch.jar!/org/apache/ivy/core/settings/ivysettings.xml
:: loading settings :: url = jar:file:/usr/local/Cellar/sbt/1.3.12/libexec/bin/sbt-launch.jar!/org/apache/ivy/core/settings/ivysettings.xml
:: loading settings :: url = jar:file:/usr/local/Cellar/sbt/1.3.12/libexec/bin/sbt-launch.jar!/org/apache/ivy/core/settings/ivysettings.xml
:: loading settings :: url = jar:file:/usr/local/Cellar/sbt/1.3.12/libexec/bin/sbt-launch.jar!/org/apache/ivy/core/settings/ivysettings.xml
:: loading settings :: url = jar:file:/usr/local/Cellar/sbt/1.3.12/libexec/bin/sbt-launch.jar!/org/apache/ivy/core/settings/ivysettings.xml
:: loading settings :: url = jar:file:/usr/local/Cellar/sbt/1.3.12/libexec/bin/sbt-launch.jar!/org/apache/ivy/core/settings/ivysettings.xml
:: loading settings :: url = jar:file:/usr/local/Cellar/sbt/1.3.12/libexec/bin/sbt-launch.jar!/org/apache/ivy/core/settings/ivysettings.xml
:: loading settings :: url = jar:file:/usr/local/Cellar/sbt/1.3.12/libexec/bin/sbt-launch.jar!/org/apache/ivy/core/settings/ivysettings.xml
downloading https://repo1.maven.org/maven2/org/scala-lang/modules/scala-xml_2.12/1.2.0/scala-xml_2.12-1.2.0.jar ...
    [SUCCESSFUL ] org.scala-lang.modules#scala-xml_2.12;1.2.0!scala-xml_2.12.jar(bundle) (3158ms)
:: retrieving :: org.scala-sbt#boot-app
    confs: [default]
    81 artifacts copied, 0 already retrieved
[info] [launcher] getting Scala 2.12.10 (for sbt)...
 ....
[info] Fetched artifacts of
[success] Total time: 189 s (03:09), completed 2020/06/01 18:55:40
% sbt ensimeConfigProject
[info] welcome to sbt 1.3.12 (N/A Java 13.0.2)
[info] loading settings for project global-plugins from plugins.sbt ...
[info] Fetching artifacts of
....

補完とハイライトの確認

scalaファイルを開くとハイライトが入り、ステータスが[ensime] Analyzer is readyとなった後にコード補完が実行されていれば完了です。

あとがき

vimでのscala編集環境設定の記事がいずれもそれなりに時間が経っていることから、うまく再現できるかどうかというところでしたが問題はなかったようです。

意外と時間がかかることだけは判ったため、必要になりそうであれば合間をみて先に行うことをおすすめします。

参考リンク

Share this article

facebook logohatena logotwitter logo

© Classmethod, Inc. All rights reserved.